package com.git.bds.mr.login.service.user;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.git.bds.mr.enums.Role;
import com.git.bds.mr.exception.BusinessException;
import com.git.bds.mr.framework.redis.constant.RedisConstants;
import com.git.bds.mr.framework.redis.util.RedisUtils;
import com.git.bds.mr.login.mapper.LoginMapper;
import com.git.bds.mr.login.model.domain.Login;
import com.git.bds.mr.login.model.dto.LoginDTO;
import com.git.bds.mr.patient.mapper.PatientInfoMapper;
import com.git.bds.mr.patient.model.domain.PatientInfo;
import com.git.bds.mr.result.ResultCode;
import com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 成大事
 * @since 2022-09-17 21:05:28
 */
@Slf4j
@Service
public class LoginServiceImpl extends MPJBaseServiceImpl<LoginMapper, Login> implements LoginService {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private PatientInfoMapper patientInfoMapper;

    /**
     * 登录
     *
     * @param info 信息
     * @param type 类型
     */
    public String login(LoginDTO info,int type) {
        Login login= this.baseMapper.selectOne(new LambdaQueryWrapper<Login>()
                    .select(Login::getPassword, Login::getId,Login::getBelongId)
                    .eq(Login::getAccount, info.getAccount())
                    .eq(Login::getRoleType,type));
        login = Optional.ofNullable(login)
                .orElseThrow(() -> new BusinessException(ResultCode.NOT_EXIST.getCode(), ResultCode.NOT_EXIST.getMessage()));
        if(login.getPassword().equals(info.getPassword())){
            Long belongId = login.getBelongId();
            log.info("belongId:  " + belongId);
            StpUtil.login(belongId);
            return StpUtil.getTokenValue();
        }else {
            throw new BusinessException(ResultCode.PWD_ERROR.getCode(), ResultCode.PWD_ERROR.getMessage());
        }

    }

    /**
     * 用户登录
     *
     * @param info 信息
     */
    @Override
    public String userLogin(LoginDTO info) {
        return login(info, Role.PATIENT.getValue());
    }

    /**
     * 医生登录
     *
     * @param info 信息
     */
    @Override
    public String doctorLogin(LoginDTO info) {
        return login(info,Role.DOCTOR.getValue());
    }

    /**
     * 医院登录
     *
     * @param info 信息
     */
    @Override
    public String hospitalLogin(LoginDTO info) {
        return login(info,Role.HOSPITAL.getValue());
    }

    /**
     * 登记
     *
     * @param tel  电话
     * @param pwd  普华永道
     * @param code 密码
     * @return {@link Boolean}
     */
    @Override
    public Boolean register(String tel, String pwd, String code) {
        String redisKey = RedisConstants.REGISTER_PHONE_KEY + tel;
        if(redisUtils.hasKey(redisKey)){
            if(code.equals(redisUtils.get(redisKey))){
                Login one = this.baseMapper.selectOne(new LambdaQueryWrapper<Login>()
                        .select(Login::getId)
                        .eq(Login::getRoleType, Role.PATIENT.getValue())
                        .eq(Login::getAccount, tel));
                if(one != null){
                    throw new BusinessException(ResultCode.REGISTERED.getCode(),ResultCode.REGISTERED.getMessage());
                }
                long patientId = IdUtil.getSnowflakeNextId();
                log.info("patientId:   " + patientId);
                PatientInfo patientInfo = new PatientInfo().setId(patientId);
                patientInfoMapper.insert(patientInfo);
                Login login = new Login().setAccount(tel)
                        .setPassword(pwd)
                        .setBelongId(patientId)
                        .setRoleType(Role.PATIENT.getValue());
                return this.baseMapper.insert(login) > 0;
            }else {
                //验证码错误
                throw new BusinessException(ResultCode.CAPTCHA_ERROR.getCode(), ResultCode.CAPTCHA_ERROR.getMessage());
            }
        }else {
            //如果redis没有这个验证码   过期了。重新发送
            throw new BusinessException(ResultCode.CAPTCHA_EXPIRE.getCode(), ResultCode.CAPTCHA_EXPIRE.getMessage());
        }
    }
}
